<template>
  <div class="app-container">
    <div class="filterDiv">
      <el-input v-model="listQuery.supplierName" size="small" placeholder="供应商代码/名称" style="width: 200px;" class="filter-item" />
      <el-button size="small" type="primary" round @click="handleFilter">查询</el-button>
      <el-button v-permiss="['60201070']" size="small" type="primary" round style="float:right;" @click="showImport">导入</el-button>
      <el-button v-permiss="['60201020']" style="float:right;margin-left:15px" size="small" type="primary" round @click="handleAdd">新增</el-button>
    </div>
    <div class="contentDiv">
      <el-table :key="tableKey" v-loading="listLoading" :data="tableData" fit highlight-current-row style="width: 100%;" size="small">
        <el-empty slot="empty" :image-size="120" description="暂无数据" />
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column label="供应商代码" align="center">
          <template slot-scope="{row}">
            <span>{{ row.supplierCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="供应商名称" align="center">
          <template slot-scope="{row}">
            <span>{{ row.supplierName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="简称" align="center">
          <template slot-scope="{row}">
            <span>{{ row.abbr }}</span>
          </template>
        </el-table-column>
        <el-table-column label="联系人" align="center">
          <template slot-scope="{row}">
            <span>{{ row.contact }}</span>
          </template>
        </el-table-column>
        <el-table-column label="电话" align="center">
          <template slot-scope="{row}">
            <span>{{ row.tel }}</span>
          </template>
        </el-table-column>
        <el-table-column label="微信号" align="center">
          <template slot-scope="{row}">
            <span>{{ row.wechat }}</span>
          </template>
        </el-table-column>
        <el-table-column label="开户行" align="center">
          <template slot-scope="{row}">
            <span>{{ row.bank }}</span>
          </template>
        </el-table-column>
        <el-table-column label="银行账号" align="center">
          <template slot-scope="{row}">
            <span>{{ row.account }}</span>
          </template>
        </el-table-column>
        <el-table-column label="预付款余额" align="center">
          <template slot-scope="{row}">
            <span>{{ row.advpBalance | fixed }}</span>
          </template>
        </el-table-column>
        <el-table-column label="应付款余额" align="center">
          <template slot-scope="{row}">
            <span>{{ row.overdraftBalance | fixed }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否启用" align="center">
          <template slot-scope="{row}">
            <el-switch v-model="row.isDisable" :active-value="0" :inactive-value="1" @change="updateStatus($event, row)" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="{row}">
            <el-button v-permiss="['60201030']" type="text" size="small" @click="handleCompile(row)">编辑</el-button>
            <el-button v-permiss="['60201060']" type="text" size="small" @click="handleDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <el-dialog :close-on-click-modal="false" :title="dialogStatus=='create'?'新增供应商':'修改供应商'" :visible.sync="dialogVisible" width="650px">
      <el-form ref="dataForm" :rules="rules" :model="form" :inline="true" label-position="right" label-width="100px" style="width: 610px; margin-left:10px;">
        <el-form-item label="供应商代码" prop="supplierCode">
          <el-input v-model="form.supplierCode" placeholder="供应商代码" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="供应商名称" />
        </el-form-item>
        <el-form-item label="简称" prop="abbr">
          <el-input v-model="form.abbr" placeholder="简称" />
        </el-form-item>
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="联系人" />
        </el-form-item>
        <el-form-item label="电话" prop="tel">
          <el-input v-model="form.tel" placeholder="电话" maxlength="11" oninput="value=value.replace(/[^\d]/g,'')" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="邮箱" />
        </el-form-item>
        <el-form-item label="微信号" prop="wechat">
          <el-input v-model="form.wechat" placeholder="微信号" />
        </el-form-item>
        <el-form-item label="开户行" prop="bank">
          <el-input v-model="form.bank" placeholder="开户行" />
        </el-form-item>
        <el-form-item label="银行账号" prop="account">
          <el-input v-model="form.account" placeholder="银行账号" oninput="value=value.replace(/[^\d.]/g,'')" />
        </el-form-item>
        <el-form-item label="预付款余额" prop="advpBalance">
          <el-input v-model="form.advpBalance" placeholder="预付款余额" class="inpright" oninput="value=value.replace(/[^\d.]/g,'')" />
        </el-form-item>
        <el-form-item label="应付款余额" prop="overdraftBalance">
          <el-input v-model="form.overdraftBalance" placeholder="应付款余额" class="inpright" oninput="value=value.replace(/[^\d.]/g,'')" />
        </el-form-item>
        <el-form-item label="地址" prop="addr">
          <el-input v-model="form.addr" placeholder="地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="备注" />
        </el-form-item>
        <el-form-item label="是否启用" prop="isDisable">
          <el-radio v-model="form.isDisable" :label="0">是</el-radio>
          <el-radio v-model="form.isDisable" :label="1">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus == 'create'?handleCreate():handleModify()">确定</el-button>
      </div>
    </el-dialog>
    <import-data ref="importRef" type="supplier" title="供应商" />
  </div>
</template>
<script>
import { getSupplier, saveSupplier, delSupplier, updateSupplierDisabled } from '@/api/basedata'
import importData from './importData'
export default {
  name: 'BaseSupplier',
  components: { importData },
  filters: {
    fixed(val) {
      return !val ? 0 : Number(val).toFixed(2)
    }
  },
  data() {
    return {
      tableKey: 0,
      tableData: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        supplierName: ''
      },
      form: {
        supplierName: '',
        supplierCode: '',
        abbr: '',
        contact: '',
        tel: '',
        email: '',
        wechat: '',
        advpBalance: '',
        overdraftBalance: '',
        bank: '',
        account: '',
        addr: '',
        remarks: '',
        isDisable: 0
      },
      dialogVisible: false,
      dialogStatus: '',
      rules: {
        supplierName: [
          { required: true, message: '供应商名称不能为空', trigger: 'change' }
        ],
        supplierCode: [
          { required: true, message: '供应商代码不能为空', trigger: 'change' }
        ],
        abbr: [
          { required: false, message: '供应商简称不能为空', trigger: 'change' }
        ],
        contact: [
          { required: false, message: '联系人不能为空', trigger: 'change' }
        ],
        tel: [{ required: false, message: '电话不能为空', trigger: 'change' }],
        email: [
          { required: false, message: '邮箱地址不能为空', trigger: 'change' }
        ],
        wechat: [
          { required: false, message: '微信号不能为空', trigger: 'change' }
        ],
        bank: [
          { required: false, message: '开户行不能为空', trigger: 'change' }
        ],
        account: [
          { required: false, message: '银行账号不能为空', trigger: 'change' }
        ],
        addr: [{ required: false, message: '地址不能为空', trigger: 'change' }],
        isDisable: [{ required: false }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    showImport() {
      this.$refs.importRef.option = {
        show: true
      }
    },
    getList() {
      this.listLoading = true
      getSupplier(this.listQuery).then((response) => {
        this.listLoading = false
        this.tableData = response.data.data
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleAdd(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'create'
      this.form.id = ''
      for (var key in this.form) {
        this.form[key] = ''
      }
      this.form.isDisable = 0
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleCompile(obj) {
      this.dialogVisible = true
      this.dialogStatus = 'update'
      for (var key in this.form) {
        this.form[key] = obj[key]
      }
      this.form.id = obj.id
      this.form.isDisable = Number(obj.isDisable)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    updateStatus(e, row) {
      this.changeAvaiable(e, row.id)
    },
    changeAvaiable(isDisable, id) {
      var obj = { isDisable, id }
      updateSupplierDisabled(obj).then((res) => {
        if (res.data.errorCode === '0') {
          this.$message.success(res.data.msg)
        }
      })
    },
    handleDel(id) {
      this.$confirm('确定删除？', '提示', {
        confirmButtonText: '确定',
        closeOnClickModal: false,
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delItem(id)
      })
    },
    handleModify() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveSupplier(this.form).then((res) => {
            if (res.data.errorCode === '0') {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    handleCreate() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveSupplier(this.form).then((res) => {
            if (res.data.errorCode === '0') {
              this.getList()
              this.dialogVisible = false
              this.$message.success(res.data.msg)
            }
          })
        }
      })
    },
    delItem(id) {
      delSupplier(id).then((res) => {
        if (res.data.errorCode === '0') {
          this.getList()
          this.dialogVisible = false
          this.$message.success(res.data.msg)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.fileDiv {
  width: 400px;
  height: 60px;
  border: 1px #d0d0d0 solid;
  line-height: 60px;
  text-align: center;
  border-radius: 6px;
}
.filter-item {
  position: relative;
}
/deep/.el-form-item {
  margin-bottom: 18px;
}
</style>
